package main

import (
	"gitee.com/simple-set/simple.io/src/event"
	"gitee.com/simple-set/simple.io/src/process/http"
	"gitee.com/simple-set/simple.io/src/process/socks"
	"log"
	"log/slog"
)

type Example struct {
	bootServer *event.BootServer
	bootClient *event.BootClient
}

func NewServer() *Example {
	return &Example{bootServer: event.NewBootServer(), bootClient: event.NewBootClient()}
}

func (s *Example) httpServer() error {
	s.bootServer.Register(http.NewRequestProcess())
	s.bootServer.Register(NewHttpServer())
	s.bootServer.Register(http.NewResponseProcess())
	return s.bootServer.ListenTcp(":8080")
}

func (s *Example) httpProxy() error {
	s.bootServer.Register(http.NewRequestProcess())
	s.bootServer.Register(NewHttpProxy())
	return s.bootServer.ListenTcp(":8080")
}

func (s *Example) socksServer() error {
	process := socks.NewProcess()
	process.SetConnect(NewConnectHandler())
	process.SetAssociate(NewAssociateHandler())
	process.AddInterceptor(newBaiduInterceptor())
	process.AddAuthenticator(NewAuthUserPass())
	process.SetNoAuth(true)
	s.bootServer.Register(process)
	return s.bootServer.ListenTcp(":8080")
}

func (s *Example) echoServerUDP() error {
	s.bootServer.Register(NewEchoServer())
	return s.bootServer.ListenUdp(":8080")
}

func (s *Example) echoServerTcp() error {
	s.bootServer.Register(NewEchoServer())
	return s.bootServer.ListenTcp(":8080")
}

func (s *Example) echoClientUDP() error {
	s.bootClient.Register(NewEchoClient())
	if starterClient, err := s.bootClient.DialUdp("127.0.0.1:8080"); err == nil {
		_, err = starterClient.Request(nil)
		return err
	} else {
		return err
	}
}

func main() {
	slog.SetLogLoggerLevel(slog.LevelDebug)
	server := NewServer()
	if err := server.echoServerTcp(); err != nil {
		log.Fatalln(err)
	}
}
